package com.boe.server.register.security.handler;

import com.boe.server.register.util.ResultUtil;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * 用户登出类
 *
 * @author Sans
 * @since 2019/10/3 9:42
 */
@Component
public class UserLogoutSuccessHandler implements LogoutSuccessHandler {

    private final ResultUtil resultUtil;

    public UserLogoutSuccessHandler(ResultUtil resultUtil) {
        this.resultUtil = resultUtil;
    }

    /**
     * 用户登出返回结果
     * 这里应该让前端清除掉Token
     *
     * @author uaniheng
     * @since 2019/10/3 9:50
     */
    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
        Map<String, Object> resultData = new HashMap<>();
        resultData.put("code", "200");
        resultData.put("msg", "登出成功");
//        SecurityContextHolder.clearContext();
        resultUtil.responseJson(response, resultData);
    }
}